//
//  loginView.m
//  iShanggang
//
//  Created by aiShangGang on 2017/3/30.
//  Copyright © 2017年 aishanggang. All rights reserved.
//

#import "LoginView.h"
#import "UIColor+Hex.h"
//#import "BOAssistor.h"

@interface LoginView ()<UITextFieldDelegate>

@property (nonatomic ,weak) UIButton *accountPasswordBtn;
@property (nonatomic ,weak) UIButton *phoneNumberBtn;
@property (nonatomic ,weak) UIView *lineView1;
@property (nonatomic ,weak) UIView *lineView2;
@property (nonatomic ,weak) UILabel *accontLbl;
@property (nonatomic ,weak) UILabel *passwordLbl;
@property (nonatomic ,weak) UIButton *eyeBtn;
@property (nonatomic ,weak) UIView *accountView;
@property (nonatomic ,weak) UIView *passwordView;
@property (nonatomic ,weak) UIView *longLineView;
@property (nonatomic ,weak) UIButton *loginBtn;
@property (nonatomic ,weak) UIButton *forgetPasswordBtn;
@property (nonatomic ,weak) UIButton *redomRunBtn;
@property (nonatomic ,weak) UIButton *protocalBtn;

@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIView *accountV = [[UIView alloc]init];
        accountV.backgroundColor = [UIColor whiteColor];
        [self addSubview:accountV];
        self.accountView = accountV;
        
        UILabel *accountLbl = [self titleText:@"账号" view:accountV];
        self.accontLbl = accountLbl;
        UITextField *accountTextField = [self placeHolder:@"请输入爱上岗账号" view:accountV];
        accountTextField.tag = 1;
        accountTextField.textColor = [UIColor colorWithHex:0x333333];
        [accountTextField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
        accountTextField.keyboardType = UIKeyboardTypeASCIICapable;
        self.accountField = accountTextField;
        
        UIView *passwordV = [[UIView alloc]init];
        passwordV.backgroundColor = [UIColor whiteColor];
        [self addSubview:passwordV];
        self.passwordView = passwordV;
        
        UILabel *passwordLbl = [self titleText:@"密码" view:passwordV];
        self.passwordLbl = passwordLbl;
        
        UITextField *passwordTextField = [self placeHolder:@"请输入密码" view:passwordV];
        passwordTextField.tag = 2;
        passwordTextField.textColor = [UIColor colorWithHex:0x333333];
        passwordTextField.delegate = self;
        passwordTextField.secureTextEntry = YES;
        [passwordTextField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
        passwordTextField.keyboardType = UIKeyboardTypeASCIICapable;
        self.passwordField = passwordTextField;
        
        UIButton *eyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [eyeBtn setImage:[UIImage imageNamed:@"PC-password-no"] forState:UIControlStateNormal];
        [eyeBtn setImage:[UIImage imageNamed:@"PC- password-can-see"] forState:UIControlStateSelected];
        [eyeBtn addTarget:self action:@selector(openAndCloseEye:) forControlEvents:UIControlEventTouchUpInside];
        [passwordV addSubview:eyeBtn];
        self.eyeBtn = eyeBtn;
        
        UIView *longLine = [[UIView alloc]init];
        longLine.backgroundColor = cCommonLineColor;
        [self addSubview:longLine];
        self.longLineView = longLine;
        
        UIButton *loginBtn = [[UIButton alloc]init];
        loginBtn.backgroundColor = [UIColor colorWithHex:0xC4C4C4];
        [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        loginBtn.layer.cornerRadius = 5;
        loginBtn.layer.masksToBounds = YES;
        loginBtn.userInteractionEnabled = NO;
        [loginBtn addTarget:self action:@selector(didClickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginBtn];
        self.loginBtn = loginBtn;
    }
    return self;
}

- (UIButton *)title:(NSString *)title color:(long)color {
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithHex:color] forState:UIControlStateNormal];
    [self addSubview:button];
    return button;
}

- (UILabel *)titleText:(NSString *)titleText view:(UIView *)View {
    UILabel *label = [[UILabel alloc]init];
    label.text = titleText;
    label.textColor = [UIColor colorWithHex:0x333333];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:15];
    [View addSubview:label];
    return label;
}

- (UITextField *)placeHolder:(NSString *)placeHolder view:(UIView *)passwordV {
    UITextField *textFiled = [[UITextField alloc]init];
    textFiled.placeholder = placeHolder;
    [textFiled setValue:[UIColor colorWithHex:0x999999] forKeyPath:@"_placeholderLabel.textColor"];
    textFiled.textColor = [UIColor colorWithHex:0x333333];
    textFiled.font = [UIFont systemFontOfSize:15];
    [passwordV addSubview:textFiled];
    return textFiled;
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.accountView.frame = CGRectMake(0,  10, self.bounds.size.width, 44);
    
    [self.accontLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountView).offset(15);
        make.top.equalTo(self.accountView);
        make.height.equalTo(@44);
    }];
    
    [self.accountField
     mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.equalTo(self.accontLbl.mas_right).offset(30);
         make.top.bottom.equalTo(self.accountView);
         make.width.equalTo(@(Width - 80));
     }];
    
    [self.longLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self);
        make.top.equalTo(self.accountView.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    self.passwordView.frame = CGRectMake(0, CGRectGetMaxY(self.accountView.frame), self.bounds.size.width, 44);
    
    [self.passwordLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.accontLbl);
        make.top.bottom.equalTo(self.passwordView);
    }];
    
    [self.eyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(- 20);
        make.centerY.equalTo(self.passwordView.mas_centerY);
    }];
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.accountField);
        make.top.bottom.equalTo(self.passwordView);
        make.width.equalTo(@(Width - 135));
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordView.mas_bottom).offset(30);
        make.height.equalTo(@44);
        make.leading.equalTo(self.passwordLbl);
        make.right.equalTo(self.mas_right).offset(- 15);
    }];
}

#pragma mark - button target
 
- (void)didClickLoginBtn {
    if ([self.delegate respondsToSelector:@selector(didClickLoginBtn)]) {
        [self.delegate didClickLoginBtn];
    }
}

- (void)openAndCloseEye:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.passwordField.secureTextEntry = NO;
    }else if (sender.selected == NO) {
        self.passwordField.secureTextEntry = YES;
    }
}

#pragma mark - textfield
- (void)changeText:(UITextField *)textField {
    if (self.accountField.text.length > 0 && self.passwordField.text.length > 0) {
        self.loginBtn.backgroundColor = [UIColor colorWithHex:0xd23023];
        self.loginBtn.userInteractionEnabled = YES;
    }else {
        self.loginBtn.backgroundColor = [UIColor colorWithHex:0xCCCCCC];
        self.loginBtn.userInteractionEnabled = NO;
    }
}

@end
